;(function(request, jdbcConn){
	var url_regexp = /^\/|\/$/g;
	/**
	 * 获取请求路由
	 */
	function getServiceUrl(){
		var url = request.getRequestURI(); // 完整的请求路径
		url = String(url);
		
		// 接口前缀，这部分将要减去的。以实体名称为界的。
		if(url.indexOf('admin_service') != -1){
			// 后端管理的接口
			url = url.replace(request.getContextPath() + "/admin_service", '');
		}else{
			url = url.replace(request.getContextPath() + "/service", '');
		}
		
		if(url == '')url = '/'; // 根
		if(url.length > 1 && url_regexp.test(url)){
			url = url.replace(url_regexp, ''); // url 最后一个斜杠应该是无用的，删除之
		}
		
		println(Constant.ConsoleDiver);
		println('Starting handle Request' + Constant.printObject_or_status + url);
		return url;
	}
	
	/**
	 * 路由分发
	 */
	this.url_parser = function (){
		var url = getServiceUrl();
	
		if(/^\w+$/.test(url)){				// 形如 /news/
			var entry = url.match(/^(\w+)$/).pop();
			entry = getService(entry);
			entry && getAction(function(){ // GET 是读取列表，POST 是创建新的实体
				 entry.findList();
			}, entry, false, false);
		}else if(/^\w+\/\d+$/.test(url)){	// 形如 /news/10001
			var arr = url.match(/^(\w+)\/(\d+)$/);
			var entry = arr[1], id = Number(arr.pop());
			entry = getService(entry);
			
			getAction(function(){
				entry.findById(id);
			}, false, {entry : entry, id : id}, {entry : entry, id : id});
		}else if(/^\w+\/\w+$/.test(url)){	// 形如 /news/img
			var arr = url.match(/^(\w+)\/(\w+)$/);
			var entry = arr[1], subObject = arr[2];
			
			entry = getService(entry);
			subObject = getService(subObject);
			if(subObject && entry){
				subObject.parentTableName = entry.getTableName();
				getAction(function(){ // GET 是读取列表，POST 是创建新的实体
					subObject.findList();
				}, subObject, false, true);
			}else{
				// 自定义操作
				if(entry[arr[2]]){
					entry[arr[2]](); // 执行
				}else{
					request.setAttribute('errMsg', '你的请求不合法：' + url);
				}
			}
		}else if(/^\w+\/\w+\/\d+$/.test(url)){	
											// 形如 /news/img/10002
			var arr = url.match(/^(\w+)\/(\w+)\/(\d+)$/);
			var entry = arr[1], subObject = arr[2], id = Number(arr[3]);
			
			subObject = getService(subObject);
//			subObject.parentTableName = getService(entry).getTableName();
			
			getAction(function(){
				subObject.findById(id);				
			}, false, {entry : subObject, id : id}, {entry : subObject, id : id});
		}else if(/^\w+\/\d+\/\w+$/.test(url)){	
											// 形如 /news/2/img
											// 该接口只有读取列表操作，没有 CUD
			var arr = url.match(/^(\w+)\/(\d+)\/(\w+)$/);
			var entry = arr[1], id = arr[2], subObject = arr[3];
			
			subObject = getService(subObject);
			subObject.parentTableName = getService(entry).getTableName();
			getAction(function(){ // 列表只有 GET
				subObject.findList(' parentId = ' + id);
			});
		}else{
			println('不能获取资源名称，无法进行操作！');
		}
	}
	var _class = arguments.callee;
	bf_controller_init = function(request, jdbcConn){
		return new _class(request, jdbcConn).url_parser();
	};
	
	function getAction(GET_Action, CREATE_Action, UPDATE_Action, DELETE_Action){
		switch(String(request.getMethod())/* JAVA String 强转到 JS Str*/){
			case 'GET' :
				return GET_Action();
			break;
			case 'POST':
				if(!CREATE_Action)throw '禁止 POST 操作！';
				var entry = CREATE_Action;
				entry.create();
			break;
			case 'PUT':
				if(UPDATE_Action === false)throw '禁止 PUT 操作！' + arguments;
				var entry = UPDATE_Action.entry, id = UPDATE_Action.id;
				entry.update(id);
			break;
			case 'DELETE':  
				if(!DELETE_Action)throw '禁止 DELETE 操作！';
				var entry = DELETE_Action.entry, id = DELETE_Action.id;
				(entry)['delete'](id);
			break;
		}
	}
	
	/**
	 * 根据 urlMapping 返回 Java 业务类
	 */
	function getService(entryStr){
		var fullClassName = urlMapping.get(entryStr);
		
		println(Constant.ConsoleDiver);

		if (fullClassName != null){
			println('找到业务规则' + Constant.printObject_or_status + fullClassName);
		
			var service = Packages.ajaxjs.lang.Bridge.newInstanceByClassName(fullClassName);
			
			if(service == null){
				var errMsg = '程序错误：实例化失败！找不到：' + fullClassName + '你是否定义了这个 Class？或检测类注入';
				println(errMsg);
				request.setAttribute('errMsg', errMsg);
				return null;
			}else{
				service.setObjs(request, jdbcConn);
				return service;
			}
		}else{
			// 这里不作异常处理
			println('没有在 urlMapping 配置中找到 ' + entryStr +'实体，可能是自定义操作，也可能是没有配置 java 注解。');
		}
		
		return null;
	}
})();